package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    // 注入业务层对象
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result queryEmp(
            String name,
            Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageResult<Emp> pageResult = empService.queryEmp(name, gender, begin, end, page, pageSize);

        return Result.success(pageResult);
    }

    /**
     * @param emp 1 员工的基本数据   2 员工的工作经历数据  List<EmpExpr> exprList;
     * @return
     */
    @PostMapping
    public Result insertEmp(@RequestBody Emp emp) throws Exception {
        empService.insertEmp(emp);

        return Result.success();
    }


    @DeleteMapping
    public Result deleteEmp(@RequestParam List<Integer> ids) {
        empService.deleteEmp(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result queryEmpById(@PathVariable Integer id) {
        Emp emp = empService.queryEmpById(id);
        return Result.success(emp);
    }
    @GetMapping("/list")
    public Result queryAllEmp(){
        List<Emp> list = empService.queryAllEmp();
        return Result.success(list);
    }

    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        empService.updateEmp(emp);
        return Result.success();
    }
}











